# OperationsOnList.py # # Description: Either returns # - the sum of the numbers in a list # - the product of the numbers in a list # # Anne Lavergne # Date: Feb. 26 2024 def performOpOnList(aList, anOperation = "+"): """Returns either the sum or the product of the numbers in a list depending on the value of the operator 'anOperation'. Returns 0 if 'anOperation' is not '+' or '*'.""" # Figure out which operation to perform on the numbers in "aList" if anOperation == "+": # Initialize the accumulator variable to 0 # We use "0" because we are adding numbers result = 0 # For each number of the list for number in aList: # Add the number to the accumulator (running sum) result += number elif anOperation == "*": # Initialize the accumulator variable to 1 # We use "1" because we are multiplying numbers result = 1 # For each number of the list for number in aList: # Add the number to the accumulator (running sum) result *= number else: result = "None" # Once done, return the result produced by this function: # the sum or product of all the numbers in the list # or 0 if 'anOperation' is not '+' or '*' return result #*** Main part of my program # Create a list theList = [2,3,4] # Test Case #1: # Call performOpOnList with this list as an argument theOp = "+" theResult = performOpOnList(theList ) # And print its result i.e., the sum of the numbers in this list print(F"The result of performing {theOp} of the list {theList} is {theResult}.") # Test Case #2: # Call performOpOnList with this list as an argument theOp = "*" theResult = performOpOnList(theList, theOp) # And print its result i.e., the product of the numbers in this list print(F"The result of performing {theOp} of the list {theList} is {theResult}.") # Test Case #3: # Call performOpOnList with this list as an argument theOp = "$" theResult = performOpOnList(theList, theOp) # And print its result i.e., the product of the numbers in this list print(F"The result of performing {theOp} of the list {theList} is {theResult}.")